package  
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author DarkGoodWIN
	 */
		
	 
	public class TBall extends TPictureControl
	{		
		public var IsFriendBall: Boolean = false;
		public var FriendName: String = "";
		public var Price: int = 10;
		
    	public static var StateOnDesk: int = 1;
    	public static var StateOnGlass: int = 2;
    	public static var StateOnUpperGlass: int = 3;
		
		public var GlassNumber: int = 0;

		public var GameInfo: TNaperstokInfo = null;
		
		public var InDrag: Boolean = false;
		private var OldInd: int = 0;
		
		private var OrgX: int;
		private var OrgY: int;
		
		private var TextInfo: Ttext = null;
		
		private var fState: int = StateOnDesk;

		public function set State(V: int): void
		{
			fState = V;
			if (V == StateOnDesk)
			{
				X = OrgX;
			    Y = OrgY;
			}
			SetTextInfo();
		}
		
		public function get State(): int
		{
			return fState;
		}
		
		private var fGlassInd: int = -1;
		private var fNextGlassInd: int = -1;
		
		//todo: fix (use server response)
		private function OnUpdateGlassInd(O: Object): void
		{
			fGlassInd = fNextGlassInd;
        	GameInfo.Glass1.SetAlfa(false);
			GameInfo.Glass2.SetAlfa(false);
			GameInfo.Glass3.SetAlfa(false);
			if (fGlassInd == 1)
			  GameInfo.Glass1.SetAlfa(true)
			else if (fGlassInd == 2)
			  GameInfo.Glass2.SetAlfa(true)
			else if (fGlassInd == 3)
			  GameInfo.Glass3.SetAlfa(true)
		}
		
		public function GlassInd(): int
		{
			return fGlassInd;
		}
		
		public function UpdateGlassIndToServer(V: int): void
		{
			fNextGlassInd = V;
			GameInfo.UpdateBallPosToServ(V, OnUpdateGlassInd);
		}
		
		public function SetTextInfo(): void
		{
			Visible = (!((fState == StateOnGlass) && (IsFriendBall)));
			if (!(IsFriendBall))
			{
				if (fState == StateOnDesk)
					TextInfo.Text = "Перетащите шарик в стакан"
				else if (fState == StateOnUpperGlass)
					TextInfo.Text = "Опустите стакан, чтобы поместить шарик"
				else if (fState == StateOnGlass)
					TextInfo.Text = "Шарик спрятан (ставка " + Price.toString() + ")";
			} else
			{				
				if (fState == StateOnDesk)
					TextInfo.Text = "Шарик не спрятан"
				else if (fState == StateOnUpperGlass)
					TextInfo.Text = "Вы выиграли!!! " + Price.toString() + " бублей";
				else if (fState == StateOnGlass)
					TextInfo.Text = "Выберите стакан (ставка " + Price.toString() + ")";
			}
		}
		
		public function Return2Desk(): void
		{
			  if (fState != StateOnDesk)	
				UpdateGlassIndToServer( -1);
			  State = StateOnDesk;
		}
		
		private function EndDrag(e: MouseEvent): void
		{			
			InDrag = false;
			(Ctrl as Sprite).stopDrag();
			Ctrl.parent.setChildIndex(Ctrl, OldInd);
			if (!(GameInfo.BallPlaced(e)))
				Return2Desk();
		}
		
		private function OnMDown(e: MouseEvent): void
		{
			if (IsFriendBall)
			  return;
			InDrag = true;
			Ctrl.parent.addChild(Ctrl);
			(Ctrl as Sprite).startDrag();
		}
		
		private function OnMUp(e: MouseEvent): void
		{	
			if (IsFriendBall)
			  return;
			EndDrag(e);
		}
		
		private function OnMMove(e: MouseEvent): void
		{
			if (InDrag)
				GameInfo.BallMoved(e);
		}
		
		public function TBall(Parent: TControl, x: int, y: int, aGameInfo: TNaperstokInfo, aTextInfo: Ttext) 
		{
			super (res.ball, Parent, x, y);
			OldInd = Ctrl.parent.getChildIndex(Ctrl);
			TextInfo = aTextInfo;
			Width = 60;
			Height = 60;	
			OrgX = x;
			OrgY = y;
			GameInfo = aGameInfo;
			State = StateOnDesk;
			addEventListener(MouseEvent.MOUSE_DOWN, OnMDown);
			addEventListener(MouseEvent.MOUSE_UP, OnMUp);
			addEventListener(MouseEvent.MOUSE_MOVE, OnMMove);
		}
		
	}

}